package com.winter.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.IenterpriseFinancingMapper;
import com.winter.mapper.IonlineAppointmentMapper;
import com.winter.mapper.StandardAssetsMapper;
import com.winter.model.DictEntry;
import com.winter.model.IenterpriseFinancing;
import com.winter.model.IonlineAppointment;
import com.winter.model.StandardAssets;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
public class IonlineAppointmentService {

    @Resource
    private IonlineAppointmentMapper ionlineAppointmentMapper;
    @Resource
    private IenterpriseFinancingMapper ienterpriseFinancingMapper;
    @Resource
    private DictEntryMapper dictEntryMapper;

    @Autowired
    private StandardAssetsMapper standardAssetsMapper;


    /*
     *
     * 新增资金预约信息
     */
    public Result insertIonline(IonlineAppointment fin) {
        if (null != fin) {
            if (Strings.isNullOrEmpty(fin.getUserNo())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            if (null == fin.getProName()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入资金名称");
            }
            if (0 == fin.getInvestIntentAmount()) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入期限");
            }
            if (Strings.isNullOrEmpty(fin.getUsreName())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入姓名");
            }
            if (Strings.isNullOrEmpty(fin.getSex())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入性别");
            }
            if (Strings.isNullOrEmpty(fin.getMobile())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入电话");
            }
            if (Strings.isNullOrEmpty(fin.getEmail())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入邮箱");
            }
            if (Strings.isNullOrEmpty(fin.getProName())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入公司名称");
            }
            if (Strings.isNullOrEmpty(fin.getPosition())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入职位");
            }
            if (Strings.isNullOrEmpty(fin.getInvestIntentTime().toString())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入日期");
            }
            if (Strings.isNullOrEmpty(fin.getAppointmentSource())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择预约方式");
            }
            fin.setCreatetime(new Date());
            fin.setStatus("0");
            fin.setIsvalid("1");
            fin.setMatchingResultStatus("1");
            // 预约债券
            if (fin.getType()
                   .equals("1")) {
                if (fin.getProId() > 0) {
                    IenterpriseFinancing en = ienterpriseFinancingMapper.selectByPrimaryKey(fin.getProId());
                    if (en != null) {
                        List<IonlineAppointment> ionlineAppointment = ionlineAppointmentMapper.queryData(fin.getProId(), "1", fin.getUserNo());
                        if (ionlineAppointment.size() > 0) {
                            return Result.failure(ResultCode.PARAM_IS_BLANK, "你已经预约过啦");
                        }
                        fin.setTrustassetsType(en.getDcFinancingType());
                        fin.setTabId(Long.valueOf(en.getSubclass()));
                        ionlineAppointmentMapper.insertSelective(fin);
                        return Result.success();
                    }
                }

            } else {
                // 预约非标
                if (fin.getProId() > 0) {
                    StandardAssets standardAssets = standardAssetsMapper.selectByPrimaryKey(fin.getProId());
                    if (standardAssets != null) {
                        List<IonlineAppointment> ionlineAppointment = ionlineAppointmentMapper.queryData(fin.getProId(), "1", fin.getUserNo());
                        if (ionlineAppointment.size() > 0) {
                            return Result.failure(ResultCode.PARAM_IS_BLANK, "你已经预约过啦");
                        }
                        fin.setTrustassetsType(standardAssets.getDcFinancingType());
                        fin.setTabId(Long.valueOf(standardAssets.getSubclass()));
                        int i = ionlineAppointmentMapper.insertSelective(fin);
                        return Result.success();
                    }
                }
            }

        }
        return Result.failure(ResultCode.SYSTEM_INNER_ERROR, "添加失败");
    }

    /*
     * 我的预约资产信息
     *
     */
    public Result getIonlineList(Integer pageNumber, Integer pageSize, String userNo) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        List<IonlineAppointment> tn = ionlineAppointmentMapper.getIonlineList(userNo);
        if (tn.size() > 0) {
            for (IonlineAppointment fin : tn) {
                // 预约债券
                if (fin.getType()
                       .equals("1")) {
                    IenterpriseFinancing en = ienterpriseFinancingMapper.selectByPrimaryKey(fin.getProId());
                    getTypeName(fin, en);
                    fin.setType("1");
                    fin.setOrgName(fin.getProName());
                } else {
                    // 预约非标
                    StandardAssets standardAssets = standardAssetsMapper.selectByPrimaryKey(fin.getProId());
                    getTypeName1(fin, standardAssets);
                    fin.setOrgName(fin.getProName());
                    fin.setType("2");
                }
            }
        }
        PageInfo<IonlineAppointment> pageInfo = new PageInfo<>(tn);
        return Result.success(pageInfo);
    }


    private void getTypeName(IonlineAppointment fin1, IenterpriseFinancing en1) {
        if (en1 != null) {
            fin1.setProName(en1.getProName());
            DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", en1.getFinAmountUnit());
            if (null != ty) {
                fin1.setFeedbackContent(en1.getFinAmountShow() + ty.getDictname());
            }

            DictEntry ty3 = dictEntryMapper.get("DC_FIN_DATE_UNIT", en1.getFinDeadlineUnit());
            if (null != ty) {
                fin1.setRemark(en1.getFinDeadline() + ty3.getDictname());
            }
            fin1.setAppointmentSource(en1.getExpectAnnualYear());
        }
    }

    private void getTypeName1(IonlineAppointment fin1, StandardAssets en1) {
        if (en1 != null) {
            fin1.setProName(en1.getProName());
            DictEntry ty = dictEntryMapper.get("DC_FIN_MONEY_UNIT", en1.getFinAmountUnit());
            if (null != ty) {
                fin1.setFeedbackContent(en1.getFinAmountShow() + ty.getDictname());
            }

            DictEntry ty3 = dictEntryMapper.get("DC_FIN_DATE_UNIT", en1.getFinDeadlineUnit());
            if (null != ty) {
                fin1.setRemark(en1.getFinDeadline() + ty3.getDictname());
            }
            fin1.setAppointmentSource(en1.getExpectAnnualYear());
        }
    }

    /*
     * 取消我的预约资产信息
     *
     */
    public Result deleteIonlineList(Long id) {
        ionlineAppointmentMapper.deleteByPrimaryKey(id);
        return Result.success("取消预约成功");
    }
    
    /**
     * 修改  预约
     * @param fin
     * @return
     */
    public Result update(IonlineAppointment ion) {
       if (Strings.isNullOrEmpty(ion.getUserNo())) {
           return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
       }
       if (ion.getId() == null) {
           return Result.failure(ResultCode.PARAM_IS_BLANK, "预约id为空");
       }
       int i = ionlineAppointmentMapper.updateByPrimaryKeySelective(ion);
       if (i <= 0) {
			 return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "预约修改失败!");
       }
       return Result.success();
    }
    
    public Result getByIonlineKey(String id, String userNo,String type) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(id)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "债券id为空");
        }
        if (Strings.isNullOrEmpty(type)) {
        	return Result.failure(ResultCode.PARAM_IS_BLANK, "预约类型为空");
        }else {
			if (type.equals("1")) {
				return Result.success(ionlineAppointmentMapper.selectByFinancingKey(Long.valueOf(id), userNo));
			}else if (type.equals("2")) {
				return Result.success(ionlineAppointmentMapper.selectByStandardKey(Long.valueOf(id), userNo));
			}
		}
      return null;
    }
}

